package com.moinut.studentattendancemanager.view.teacher;

import com.moinut.studentattendancemanager.UserManager;
import com.moinut.studentattendancemanager.controller.TeacherController;
import com.moinut.studentattendancemanager.controller.callback.OnGetListener;
import com.moinut.studentattendancemanager.controller.callback.OnUpdateListener;
import com.moinut.studentattendancemanager.model.vo.db.Teacher;
import com.moinut.studentattendancemanager.view.BaseFrame;
import com.moinut.studentattendancemanager.utils.WindowJumper;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * @author MOILING
 */
public class TeacherInfoFrame extends BaseFrame<TeacherInfoFrame.TeacherInfoPanel> {
    private static final long serialVersionUID = 1L;

    private static final int DEFAULT_WIDTH = 350;
    private static final int DEFAULT_HEIGHT = 350;

    public TeacherInfoFrame() {
        super("教师信息", DEFAULT_WIDTH, DEFAULT_HEIGHT);
    }

    @Override
    protected TeacherInfoPanel onSetContentPane() {
        return new TeacherInfoPanel();
    }

    /* package */ class TeacherInfoPanel extends JPanel implements ActionListener {
        private static final long serialVersionUID = 1L;

        private JLabel mNameLabel, mIdLabel, mAcademyLabel, mStaffLabel, mEmailLabel, mPicTipLabel;
        private JTextField mNameTextField, mIdTextField, mEmailTextField, mAcademyTextField, mStaffTextField;
        private JLabel mPicLabel;
        private JButton mChangePicButton, mChangeEmailButton, mBackButton;
        private JPanel mInfoPanel, mPicPanel, mButtonPanel, mTopPanel;

        /*package*/ TeacherInfoPanel() {
            setLayout(new BorderLayout());
            initTopPanel();
            initButtonPanel();
            TeacherController.getTeacherById(UserManager.userId, new OnGetListener<Teacher>() {
                @Override
                public void onSuccess(Teacher teacher) {
                    mNameTextField.setText(teacher.getTeacherName());
                    mIdTextField.setText(teacher.getTeacherId());
                    mAcademyTextField.setText(teacher.getCollegeName());
                    mStaffTextField.setText(teacher.getFacultyName());
                    mEmailTextField.setText(teacher.getTeacherEmail());
                }

                @Override
                public void onError(String error) {
                    JOptionPane.showMessageDialog(null, error, "错误", JOptionPane.ERROR_MESSAGE);
                }
            });
        }

        private void initTopPanel() {
            mTopPanel = new JPanel(null);
            initInfoPanel();
            initPicPanel();
            add(mTopPanel, BorderLayout.CENTER);
        }

        private void initButtonPanel() {
            mChangeEmailButton = new JButton("修改个人邮箱");
            mBackButton = new JButton("返回上级菜单");

            mButtonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
            mButtonPanel.add(mChangeEmailButton);
            mButtonPanel.add(mBackButton);

            add(mButtonPanel, BorderLayout.SOUTH);

            mBackButton.addActionListener(this);
            mChangeEmailButton.addActionListener(this);
        }

        private void initPicPanel() {
            mPicTipLabel = new JLabel("个人照片");
            mPicLabel = new JLabel();
            mPicLabel.setSize(140, 140);
            ImageIcon image = new ImageIcon(this.getClass().getResource("/res/images/portrait.jpg"));
            image.setImage(image.getImage().getScaledInstance(mPicLabel.getWidth(),
                    mPicLabel.getHeight(),
                    Image.SCALE_DEFAULT));
            mPicLabel.setIcon(image);
            mChangePicButton = new JButton("修改个人照片");

            mPicPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
            mPicPanel.setBounds(200, 0, 140, 300);

            mPicPanel.add(mPicTipLabel);
            mPicPanel.add(mPicLabel);
            mPicPanel.add(mChangePicButton);

            mTopPanel.add(mPicPanel);
            mChangePicButton.addActionListener(this);
        }

        private void initInfoPanel() {
            mNameLabel = new JLabel("姓名");
            mIdLabel = new JLabel("教师编号");
            mAcademyLabel = new JLabel("所属学院");
            mStaffLabel = new JLabel("所属教研室");
            mEmailLabel = new JLabel("电子邮箱");
            mNameTextField = new JTextField(12);
            mNameTextField.setEditable(false);
            mIdTextField = new JTextField(12);
            mIdTextField.setEditable(false);
            mAcademyTextField = new JTextField(12);
            mAcademyTextField.setEditable(false);
            mStaffTextField = new JTextField(12);
            mStaffTextField.setEditable(false);
            mEmailTextField = new JTextField(12);
            mEmailTextField.setEditable(false);

            mInfoPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
            mInfoPanel.setBounds(5, 5, 160, 340);

            mInfoPanel.add(mNameLabel);
            mInfoPanel.add(mNameTextField);
            mInfoPanel.add(mIdLabel);
            mInfoPanel.add(mIdTextField);
            mInfoPanel.add(mAcademyLabel);
            mInfoPanel.add(mAcademyTextField);
            mInfoPanel.add(mStaffLabel);
            mInfoPanel.add(mStaffTextField);
            mInfoPanel.add(mEmailLabel);
            mInfoPanel.add(mEmailTextField);

            mTopPanel.add(mInfoPanel);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource().equals(mBackButton)) {
                WindowJumper.back(TeacherInfoFrame.this);
            } else if (e.getSource().equals(mChangeEmailButton)) {
                String newEmail = JOptionPane.showInputDialog(null, "修改的邮箱为");
                TeacherController.updateTeacherEmail(UserManager.userId, newEmail, new OnUpdateListener() {
                    @Override
                    public void onSuccess(String success) {
                        JOptionPane.showMessageDialog(null, success, "成功", JOptionPane.INFORMATION_MESSAGE);
                        mEmailTextField.setText(newEmail);
                    }

                    @Override
                    public void onError(String error) {
                        JOptionPane.showMessageDialog(null, error, "错误", JOptionPane.ERROR_MESSAGE);
                    }
                });
            } else if (e.getSource().equals(mChangePicButton)) {
                JFileChooser chooser = new JFileChooser();
                chooser.setFileFilter(new FileNameExtensionFilter("image files", "jpg", "jpeg", "gif", "png", "bmp"));
                chooser.setCurrentDirectory(new File("."));
                int result = chooser.showOpenDialog(TeacherInfoFrame.this);
                if (result == JFileChooser.APPROVE_OPTION) {
                    String fileName = chooser.getSelectedFile().getPath();
                    ImageIcon image = new ImageIcon(fileName);
                    image.setImage(image.getImage().getScaledInstance(mPicLabel.getWidth(),
                            mPicLabel.getHeight(),
                            Image.SCALE_DEFAULT));
                    mPicLabel.setIcon(image);
                }
            }
        }
    }
}
